﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1001
{
    [Plug(ID = "cMES1001",
          Name = "BOM资料",
          Description = "BOM资料维护",
          Parent = "0,基础数据")]
    public partial class cMES1001 : StdView
    {
        private string clickKpn = string.Empty;

        public cMES1001()
        {
            InitializeComponent();
            datalistbom.CellMouseDoubleClick += Datalist_CellMouseDoubleClick;
        }
        private void cMES1000_Load(object sender, EventArgs e)
        {
            //this.LoadVendorList();
        }

        private void LoadBomList()
        {
            btnSearch_Click(null, null);
        }

        private void LoadBomAlternativeList()
        {
            string vm = JsonConvert.SerializeObject(new
            {
                bomno = txtSearch.Text,  //bomno
                partno = clickKpn  //keypartno
            });

            ExecutionResult<dynamic> result = Services.ServiceMethod.GetBomAlternativeDataByPartNo(vm);

            if (result.Status)
            {
                datalistalternative.DataSource = result.Data;
                datalistalternative.ClearSelection();
            }
            else
            {
                this.ShowMessage("error:" + result.Message, MsgType.OK);
                return;
            }

        }

        private void Datalist_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                EditBomMain edit = new EditBomMain("EDIT", new object[]
                {
                    txtSearch.Text,//bomno
                    datalistbom.SelectedRows[0].Cells[0].Value, // kpn
                    datalistbom.SelectedRows[0].Cells[1].Value, // bomversion
                    datalistbom.SelectedRows[0].Cells[2].Value, //qty
                    datalistbom.SelectedRows[0].Cells[3].Value,//unit
                    datalistbom.SelectedRows[0].Cells[4].Value,//wastage_rate
                    datalistbom.SelectedRows[0].Cells[5].Value,//alternative
                    datalistbom.SelectedRows[0].Cells[6].Value,//sequence
                    datalistbom.SelectedRows[0].Cells[7].Value,//operaton
                    datalistbom.SelectedRows[0].Cells[8].Value,//designator_location
                    datalistbom.SelectedRows[0].Cells[9].Value, //remarks
                    datalistbom.SelectedRows[0].Cells[10].Value, //
                    datalistbom.SelectedRows[0].Cells[11].Value //
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadBomList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearch.Text.Trim()))
            {
                ShowMessage($"查询的BomNo：{txtSearch.Text}不能为空", MsgType.NG);
            }
            else
            {
                ExecutionResult<dynamic> res = Services.ServiceMethod.GetBomdataByBomNo(txtSearch.Text);

                if (!res.Status)
                {
                    ShowMessage(res.Message, MsgType.NG);
                }
                else
                {
                    if (res.Data.Count > 0)
                    {
                        datalistbom.DataSource = res.Data;

                        datalistalternative.Rows.Clear();
                    }
                    else
                    {
                        datalistbom.Rows.Clear();
                        datalistalternative.Rows.Clear();
                        ShowMessage($"没有查到Bom信息：{txtSearch.Text}", MsgType.NG);
                    }

                    datalistbom.ClearSelection();
                    datalistalternative.ClearSelection();
                }
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            EditBomMain edit = default(EditBomMain);

            if (((ToolStripButton)sender).Tag.Equals("ADD"))
            {
                edit = new EditBomMain("ADD",new object[]{ txtSearch.Text});

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据添加成功！", MsgType.OK);

                    this.LoadBomList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据添加失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("EDIT"))
            {
                if (datalistbom.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个料号信息进行编辑.", MsgType.NG);
                    return;
                }
                edit = new EditBomMain("EDIT", new object[] 
                {
                    txtSearch.Text,//bomno
                    datalistbom.SelectedRows[0].Cells[0].Value, // kpn
                    datalistbom.SelectedRows[0].Cells[1].Value, // bomversion
                    datalistbom.SelectedRows[0].Cells[2].Value, //qty
                    datalistbom.SelectedRows[0].Cells[3].Value,//unit
                    datalistbom.SelectedRows[0].Cells[4].Value,//wastage_rate
                    datalistbom.SelectedRows[0].Cells[5].Value,//alternative
                    datalistbom.SelectedRows[0].Cells[6].Value,//sequence
                    datalistbom.SelectedRows[0].Cells[7].Value,//operaton
                    datalistbom.SelectedRows[0].Cells[8].Value,//designator_location
                    datalistbom.SelectedRows[0].Cells[9].Value, //remarks
                    datalistbom.SelectedRows[0].Cells[10].Value, //is valid
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadBomList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("DELETE"))
            {
                if (datalistbom.SelectedRows.Count < 1 )
                {
                    ShowMessage("请选择一个需要删除的Bom材料信息.", MsgType.NG);
                    return;
                }

                DialogResult dialog = MessageBox.Show("确定要删除选中吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                //{datalistbom.SelectedRows[0].Cells[0].Value}
                if (dialog == DialogResult.Yes)
                {
                    if (datalistbom.SelectedRows.Count >= 1)
                    {
                        //
                        string vm = JsonConvert.SerializeObject(new
                        {
                            bomno = txtSearch.Text,  //bomno
                            partno = datalistbom.SelectedRows[0].Cells[0].Value.ToString()  //keypartno
                        });

                        ExecutionResult result = Services.ServiceMethod.DeleteBomByBomPartNo(vm);

                        if (result.Status)
                        {
                            this.ShowMessage($"数据{datalistbom.SelectedRows[0].Cells[0].Value}删除成功！", MsgType.OK);

                            this.LoadBomList();
                        }
                    }                  
                }
            }
        }


        private void EditAlternative_Click(object sender, EventArgs e)
        {
            EditBomAlternative edit = default(EditBomAlternative);

            if (((ToolStripButton)sender).Tag.Equals("ADD"))
            {
                if (datalistbom.SelectedRows.Count <1)
                {
                    ShowMessage("请选中需要增加替代料的主料信息", MsgType.NG);
                    return;
                }

                edit = new EditBomAlternative("ADD", new object[] { txtSearch.Text,clickKpn });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据添加成功！", MsgType.OK);

                    this.LoadBomAlternativeList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据添加失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("EDIT"))
            {
                if (datalistbom.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个料号信息进行编辑.", MsgType.NG);
                    return;
                }

                string xx = txtSearch.Text + ":" +//bomno
                    clickKpn + ":" +
                    datalistbom.SelectedRows[0].Cells[0].Value + ":" + // akpn
                    datalistbom.SelectedRows[0].Cells[1].Value + ":" + // 类型
                    datalistbom.SelectedRows[0].Cells[2].Value + ":" + //单位
                    datalistbom.SelectedRows[0].Cells[3].Value + ":" + //数量
                    datalistbom.SelectedRows[0].Cells[4].Value + ":" + //顺序
                    datalistbom.SelectedRows[0].Cells[5].Value + ":" + //生效日期
                    datalistbom.SelectedRows[0].Cells[6].Value + ":" + //失效日期
                    datalistbom.SelectedRows[0].Cells[7].Value + ":" + //备注
                    datalistbom.SelectedRows[0].Cells[8].Value; //是否可用

                edit = new EditBomAlternative("EDIT", new object[]
                {
                    txtSearch.Text,//bomno
                    clickKpn,
                    datalistalternative.SelectedRows[0].Cells[0].Value, // akpn   // 
                    datalistalternative.SelectedRows[0].Cells[1].Value, // 类型
                    datalistalternative.SelectedRows[0].Cells[2].Value, //单位
                    datalistalternative.SelectedRows[0].Cells[3].Value, //数量
                    datalistalternative.SelectedRows[0].Cells[4].Value, //顺序
                    datalistalternative.SelectedRows[0].Cells[5].Value, //生效日期
                    datalistalternative.SelectedRows[0].Cells[6].Value, //失效日期
                    datalistalternative.SelectedRows[0].Cells[7].Value, //备注
                    datalistalternative.SelectedRows[0].Cells[8].Value, //是否可用
                    //datalistalternative.SelectedRows[0].Cells[9].Value,  //
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadBomAlternativeList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("DELETE"))
            {
                if (datalistalternative.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个需要删除的Bom材料信息.", MsgType.NG);
                    return;
                }

                DialogResult dialog = MessageBox.Show("确定要删除选中吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (dialog == DialogResult.Yes)
                {
                    if (datalistalternative.SelectedRows.Count >= 1)
                    {
                        //
                        string vm = JsonConvert.SerializeObject(new
                        {
                            bomno = txtSearch.Text,  //bomno
                            kpn=clickKpn,
                            akpn = datalistalternative.SelectedRows[0].Cells[0].Value.ToString()  //keypartno
                        });

                        ExecutionResult result = Services.ServiceMethod.DeleteBomAlternativeByPartNo(vm);

                        if (result.Status && result.ExecuteCount > 0)
                        {
                            this.ShowMessage($"数据删除成功！", MsgType.OK);

                            this.LoadBomAlternativeList();
                        }
                    }
                }
            }
        }


        private void datalistbom_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                clickKpn = string.Empty;
                clickKpn = datalistbom.SelectedRows[0].Cells[0].Value.ToString();
                
                LoadBomAlternativeList();
                
            }
        }
    }
}
